/*
 *  @ProductName@
 *  Copyright (C) @CopyrightYear@
 *    National Institute of Advanced Industrial Science and Technology (AIST)
 *    Registration Number: @AISTRegistrationNumber@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.util.net;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restlet.Client;
import org.restlet.data.Protocol;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;




/**
 * An HTTP client using Restlet.
 *
 * @author  Akihito Nakamura, AIST
 * @version $Id: RestletHttp.java 193 2010-12-01 03:18:59Z nakamura5akihito $
 * @see     <a href="http://www.restlet.org/">Restlet</a>
 */
public class RestletHttp
{

    /**
     * Logger.
     */
    private static Log  _LOG = LogFactory.getLog( RestletHttp.class );


    public static final String  ATTR_CREATED = "http.created";
    public static final String  ATTR_STATUS_CODE = "http.status_code";
    public static final String  ATTR_STATUS_NAME = "http.status_name";



    /**
     * Default supported protocols.
     */
    public static final List<Protocol>  PROTOCOLS =
        Collections.unmodifiableList(
                        Arrays.asList( new Protocol[] { Protocol.HTTP
                                        /* , Protocol.HTTPS */
                                        } )
                    );



    private List<Protocol>  _protocols;
    private Client  _client;



    /**
     * Constructor.
     */
    public RestletHttp()
    {
        this( PROTOCOLS );
    }



    /**
     * Constructor.
     */
    public RestletHttp(
                    final List<Protocol> protocols
                    )
    {
        _protocols = new ArrayList<Protocol>( protocols );
    }



    /**
     */
    private synchronized Client _getClient()
    {
        if (_client == null) {
            _client = new Client( _protocols );
        }

        return _client;
    }



    /**
     * HTTP GET
     */
    public Response get(
                    final String url
                    )
    {
        if (_LOG.isDebugEnabled()) {
            _LOG.debug( "HTTP GET: url=" + url );
        }

        Response  response = _getClient().get( url );
        Status  status = response.getStatus();
        if (_LOG.isDebugEnabled()) {
            _LOG.debug( "HTTP GET response: status=" + String.valueOf( status ) );
        }

        return response;
    }



    /**
     * HTTP POST
     */
    public Response post(
                    final String url,
                    final Representation resource
                    )
    {
        if (_LOG.isDebugEnabled()) {
            _LOG.debug( "HTTP POST: url=" + url );
        }

        Response  response = _getClient().post( url, resource );
        Status  status = response.getStatus();

        if (_LOG.isDebugEnabled()) {
            _LOG.debug( "HTTP POST response: status=" + String.valueOf( status ) );
        }

        return response;
    }

}
// RestletHttp

/* vim:set tabstop=4:set expandtab:set shiftwidth=4: */

